package com.sz.stock.controller;


import com.sz.stock.client.StockClient;
import com.sz.stock.client.TUserClient;
import com.sz.stock.model.*;
import com.sz.stock.service.*;
import com.sz.stock.utils.ContUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

//交易系统 Controller 包括 (买入 卖出 撤销  委托 持仓 成交)
@RestController
@RequestMapping("/trading")
public class TradingController {

    /**
     *  成交Service
     */
    @Autowired
    private IDealService iDealService;

    /**
     *  委托Service
     */
    @Autowired
    private IEntrustService iEntrustService;

    /**
     *  持仓Service
     */
    @Autowired
    private IPosService iPosService;

    @Autowired
    private ContUtils contUtils;

    @Autowired
    private TUserClient tUserClient;

    @Autowired
    private IBuyOrSaleService iBuyOrSaleService;

    @Autowired
    private StockClient stockClient;

    /**
     *  股票撤销功能1
     *      在委托页面中 新增一栏-->"已撤单"
     *      将 撤单的股票信息 填写在"已撤单"栏中.
     */
    @RequestMapping("/cancel")
    public ResultBean cancelStock(@RequestBody ArrayList<Integer> stockEntrustIds){
        int n=0;
        int m=0;
        for (Integer stockEntrustId : stockEntrustIds) {
            Entrust entrust=iEntrustService.getOneEntrust(stockEntrustId);
            iBuyOrSaleService.addOrUpdate1(entrust,-1*entrust.getStockEntrustNum());
            if (entrust.getStockEntrustStatus().equals("未成交")){
                //修改状态为 "全部撤单" 委托数量=撤单数量
                n=iEntrustService.updateStatus(new Entrust(stockEntrustId,"全部撤单",null,entrust.getStockEntrustNum()-entrust.getStockEntrustTransnum()));
            } else if (entrust.getStockEntrustStatus().equals("部成")){
                //修改状态为  "部撤"   撤单数量=委托数量-成交数量
                m=iEntrustService.updateStatus(new Entrust(stockEntrustId,"部撤",null,entrust.getStockEntrustNum()-entrust.getStockEntrustTransnum()));
            }
            //把撤单的总金额返回给客户 撤单数量*委托价格(买入)
            if (entrust.getStockEntrustOperate().equals("买入")){
                int userid=entrust.getStockEntrustUserid();
                BigDecimal bignum2 = new BigDecimal(entrust.getStockEntrustNum()-entrust.getStockEntrustTransnum());
                BigDecimal money=entrust.getStockEntrustMoney().multiply(bignum2);
                tUserClient.topup(userid,money); //把钱退给客户的方法
            }
            //如果是卖出, 把客户持仓表里面的可用数量增加, 相应冻结数量减少
            if(entrust.getStockEntrustOperate().equals("卖出")){
                int userid=entrust.getStockEntrustUserid();
                String code = entrust.getStockEntrustCode();
               POS pos= iPosService.getOnePos(code,userid);
               pos.setStockPosKyblance(pos.getStockPosKyblance()+(entrust.getStockEntrustNum()-entrust.getStockEntrustTransnum()));
               pos.setStockPosDjblance(pos.getStockPosDjblance()-(entrust.getStockEntrustNum()-entrust.getStockEntrustTransnum()));
               iPosService.posUpdate(pos); //修改pos表
            }
        }
        if(n+m!=0){
            return new ResultBean(101,"撤单成功!","1");
        } else {
            return new ResultBean(101,"撤单失败!","1");
        }
    }
    /**
     *  股票撤销功能2
     *      撤全部买入 撤全部
     */
    @RequestMapping("/canel2")
    public ResultBean canelStock2(@RequestBody int userid){
        int m=0;
        int n=0;
        List<Entrust> entrustList=iEntrustService.getEntrustByOperate(userid,"买入");
        for (Entrust entrust : entrustList) {
            if (entrust.getStockEntrustStatus().equals("未成交")){
                iBuyOrSaleService.addOrUpdate1(entrust,-1*entrust.getStockEntrustNum());
                //修改状态为 "全部撤单" 委托数量=撤单数量
                n=iEntrustService.updateStatus(new Entrust(entrust.getStockEntrustId(),"全部撤单",null,entrust.getStockEntrustNum()-entrust.getStockEntrustTransnum()));
                //把撤单的总金额返回给客户 撤单数量*委托价格
                BigDecimal bignum2 = new BigDecimal(entrust.getStockEntrustNum()-entrust.getStockEntrustTransnum());
                BigDecimal money=entrust.getStockEntrustMoney().multiply(bignum2);
                tUserClient.topup(userid,money); //把钱退给客户的方法
            } else if(entrust.getStockEntrustStatus().equals("部成")){
                iBuyOrSaleService.addOrUpdate1(entrust,-1*entrust.getStockEntrustNum());
                //修改状态为  "部撤"   撤单数量=委托数量-成交数量
                m=iEntrustService.updateStatus(new Entrust(entrust.getStockEntrustId(),"部撤",null,entrust.getStockEntrustNum()-entrust.getStockEntrustTransnum()));
                //把撤单的总金额返回给客户 撤单数量*委托价格
                BigDecimal bignum2 = new BigDecimal(entrust.getStockEntrustNum()-entrust.getStockEntrustTransnum());
                BigDecimal money=entrust.getStockEntrustMoney().multiply(bignum2);
                tUserClient.topup(userid,money); //把钱退给客户的方法
            }
        }
        if(n+m!=0){
            return new ResultBean(101,"撤单成功!","1");
        } else {
            return new ResultBean(101,"撤单失败!","1");
        }
    }
    /**
     *  股票撤销功能3
     *      撤全部卖出
     */
    @RequestMapping("/canel3")
    public ResultBean canelStock3(@RequestBody int userid){
        int m=0;
        int n=0;
        List<Entrust> entrustList=iEntrustService.getEntrustByOperate(userid,"卖出");
        for (Entrust entrust : entrustList) {
            if (entrust.getStockEntrustStatus().equals("未成交")){
                iBuyOrSaleService.addOrUpdate1(entrust,-1*entrust.getStockEntrustNum());
                //修改状态为 "全部撤单" 委托数量=撤单数量
                n=iEntrustService.updateStatus(new Entrust(entrust.getStockEntrustId(),"全部撤单",null,entrust.getStockEntrustNum()-entrust.getStockEntrustTransnum()));
                //卖出, 把客户持仓表里面的可用数量增加, 相应冻结数量减少
                String code = entrust.getStockEntrustCode();
                POS pos= iPosService.getOnePos(code,userid);
                pos.setStockPosKyblance(pos.getStockPosKyblance()+(entrust.getStockEntrustNum()-entrust.getStockEntrustTransnum()));
                pos.setStockPosDjblance(pos.getStockPosDjblance()-(entrust.getStockEntrustNum()-entrust.getStockEntrustTransnum()));
                iPosService.posUpdate(pos); //修改pos表
            } else if(entrust.getStockEntrustStatus().equals("部成")){
                iBuyOrSaleService.addOrUpdate1(entrust,-1*entrust.getStockEntrustNum());
                //修改状态为  "部撤"   撤单数量=委托数量-成交数量
                m=iEntrustService.updateStatus(new Entrust(entrust.getStockEntrustId(),"部撤",null,entrust.getStockEntrustNum()-entrust.getStockEntrustTransnum()));
                //卖出, 把客户持仓表里面的可用数量增加, 相应冻结数量减少
                String code = entrust.getStockEntrustCode();
                POS pos= iPosService.getOnePos(code,userid);
                pos.setStockPosKyblance(pos.getStockPosKyblance()+(entrust.getStockEntrustNum()-entrust.getStockEntrustTransnum()));
                pos.setStockPosDjblance(pos.getStockPosDjblance()-(entrust.getStockEntrustNum()-entrust.getStockEntrustTransnum()));
                iPosService.posUpdate(pos); //修改pos表
            }
        }
        if(n+m!=0){
            return new ResultBean(101,"撤单成功!","1");
        } else {
            return new ResultBean(101,"撤单失败!","1");
        }
    }

    /**
     *  股票改单,只能修改 委托价格  系统重新生成修改时间
     */
    @RequestMapping("/updateMoney")
    public ResultBean updateMoney(@RequestBody Entrust entrust){
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String datetime = tempDate.format(new java.util.Date());
        //查询数据库里面的委托价格和用户输入的价格比较
        BigDecimal money=iEntrustService.getOneEntrust(entrust.getStockEntrustId()).getStockEntrustMoney();
        entrust.setStockEntrustTime(datetime);
        int n=iEntrustService.updateStatus(entrust);
        if (entrust.getStockEntrustOperate().equals("买入")){
            if (money.compareTo(entrust.getStockEntrustMoney())==1){
                //数据库里面的价格比用户输入的大,余额增加一点
                int userid=entrust.getStockEntrustUserid();
                BigDecimal big=new BigDecimal(entrust.getStockEntrustNum());
                BigDecimal bigDecimal=money.subtract(entrust.getStockEntrustMoney());
                BigDecimal all=big.multiply(bigDecimal);
                tUserClient.topup(userid,all); //余额增加
            }
            if (money.compareTo(entrust.getStockEntrustMoney())==-1){
                //数据库里面的价格比用户输入的小,余额减少一点
                int userid=entrust.getStockEntrustUserid();
                BigDecimal big=new BigDecimal(entrust.getStockEntrustNum());
                BigDecimal bigDecimal2=entrust.getStockEntrustMoney().subtract(money);
                BigDecimal all=big.multiply(bigDecimal2);
                tUserClient.withdraw(userid,all); //余额减少
            }
        }
        if (n>0){
            iBuyOrSaleService.addOrUpdate1(entrust,entrust.getStockEntrustNum());
            entrust.setStockEntrustMoney(money);
            iBuyOrSaleService.addOrUpdate1(entrust,-1*entrust.getStockEntrustNum());
            return new ResultBean(0,"改单成功!",1);
        } else {
            return new ResultBean(1,"改单失败",1);
        }
    }

    /**
     *  查询 所有成交信息 带分页
     */
    @RequestMapping("/allDeal2")
    public ResultBean allDeal2(int pageIndex, int pageSize,int userid, String stockDealCode){
        return new ResultBean(1,"查询成交成功,带分页",iDealService.allDeal2(userid,pageIndex,pageSize,stockDealCode));
    }

    /**
     *  添加 成交信息
     */
    @RequestMapping("/dealAdd")
    public ResultBean dealAdd(Deal deal){
        return new ResultBean(101,"保存成功",iDealService.dealAdd(deal));
    }

    /**
     * 查询 所有委托信息
     */
    @RequestMapping("/showEntrust")
    public ResultBean showEntrust(@RequestBody int userid) {
        return new ResultBean(101, "查询所有委托信息成功!", iEntrustService.showEntrust(userid));
    }

    /**
     *    卖出  (持仓 成交 委托)
     */
    @RequestMapping("/addEntrust")
    public ResultBean addEntrust(@RequestBody Entrust entrust) {  // 用户id 股票代码,股票名称,卖出价格,卖出数量
        ResultBean resultBean1=null;
        int shuliang=0;//可买数量
        Bankcard bankcard=tUserClient.selectBankCardByUserID(entrust.getStockEntrustUserid());//用户银行卡
        //     创建委托对象--参数: 用户id, 操作(卖出), 股票编码, 股票名称, 委托价, 可卖数量,委托数,委托时间, 股东账户(用户银行卡), 合同编号
        Entrust entrust1 = new Entrust(entrust.getStockEntrustUserid(),
                "卖出",
                entrust.getStockEntrustCode(),
                entrust.getStockEntrustCompany(),
                entrust.getStockEntrustMoney(),
                shuliang,
                entrust.getStockEntrustNum(),
                new SimpleDateFormat("yyy-MM-dd HH:mm:ss.SSS").format(new Timestamp(System.currentTimeMillis())),
                "未成交",
                0,
                0,
                bankcard.getBankCardnumber(),
                contUtils.getcontractid());

//        查 股票信息表 股票信息表id(委托外键)
        Stock stock=stockClient.getStock1(entrust.getStockEntrustCode());
        if(stock!=null){
            entrust1.setStockEntrustStockid(stock.getStockId());
        }

        //买卖盘信息操作
            //1.买入信息修改
        int paycount=entrust1.getStockEntrustNum();
        List<StockBuyOrSale> allBoS = iBuyOrSaleService.getAll(entrust1.getStockEntrustCode(), "买入");
        for (int i = 0; i < allBoS.size(); i++) {
            if ((paycount-allBoS.get(i).getStockBSQuantity())<=0){
                allBoS.get(i).setStockBSQuantity(allBoS.get(i).getStockBSQuantity()-paycount);
                //修改该数据
                iBuyOrSaleService.updateBos(allBoS.get(i).getStockCode(),allBoS.get(i).getStockBSType(),allBoS.get(i).getStockBSPrice(),allBoS.get(i).getStockBSQuantity());
                paycount=0;
                continue;
            } else{
                paycount=paycount-allBoS.get(i).getStockBSQuantity();
                allBoS.get(i).setStockBSQuantity(0);
                //删除该数据
                iBuyOrSaleService.deleteOne(allBoS.get(i).getStockCode(),allBoS.get(i).getStockBSType(),allBoS.get(i).getStockBSPrice());
            }
        }
            //2.卖出信息修改
            iBuyOrSaleService.addOrUpdate1(entrust1,paycount);

        //添加委托
        resultBean1=new ResultBean(101,"保存成功",iEntrustService.addEntrust1(entrust1));
        //修改持仓
        POS pos=new POS();  //创建持仓对象
        pos.setStockPosUserid(entrust1.getStockEntrustUserid());  //set用户id
        pos.setStockPosCode(entrust1.getStockEntrustCode());  //set股票代码
        ResultBean resultBean = selectPos(pos); //查询持仓表得到股票信息
        POS pos1= (POS) resultBean.getData();
        pos1.setStockPosKyblance(pos1.getStockPosKyblance()-entrust1.getStockEntrustNum()); //修改可用数量(可用-卖出)
        pos1.setStockPosDjblance(pos1.getStockPosDjblance()+entrust1.getStockEntrustNum()); //修改冻结数量(冻结+卖出)
        posUpdate(pos1); //修改或删除 持仓

        //    查买入委托(用户以外的,股票代码一样的,操作是:买入的,状态是: 未成交或部成的,委托价大于或等于的)
        List<Entrust> entrustList=iEntrustService.selectCondition(entrust1.getStockEntrustUserid(),entrust1.getStockEntrustCode(),entrust1.getStockEntrustMoney());

        //entrustList为符合成交的
        if (entrustList!=null){
            for (Entrust entrust2 : entrustList) {
                //(卖出数量-成交数量)<(委托买入数量-成交数量),
                if (entrust1.getStockEntrustNum()-entrust1.getStockEntrustTransnum()<entrust2.getStockEntrustNum()-entrust2.getStockEntrustTransnum()){
                    //[卖方全部成交 买方部分成交(停止for循环)] 修改双方: 委托信息,成交信息,持仓信息 资金
                    //  卖出方=成交
                    Deal deal=new Deal(entrust1.getStockEntrustUserid(),
                            entrust1.getStockEntrustCode(),
                            entrust1.getStockEntrustCompany(),
                            "卖出",
                            entrust1.getStockEntrustMoney(),
                            new SimpleDateFormat("yyy-MM-dd HH:mm:ss.SSS").format(new Timestamp(System.currentTimeMillis())),
                            entrust1.getStockEntrustContractid());
                    dealAdd(deal);
                    //买入方=成交entrust2
                    Deal deal1=new Deal(entrust2.getStockEntrustUserid(),
                            entrust2.getStockEntrustCode(),
                            entrust2.getStockEntrustCompany(),
                            "买入",
                            entrust2.getStockEntrustMoney(),
                            new SimpleDateFormat("yyy-MM-dd HH:mm:ss.SSS").format(new Timestamp(System.currentTimeMillis())),
                            entrust2.getStockEntrustContractid());
                    dealAdd(deal1);

//                    修改卖出方账户金额(<<<) 本次交易数量*委托金额
                    BigDecimal bigDecimal=new BigDecimal(entrust1.getStockEntrustNum()-entrust1.getStockEntrustTransnum());
                    bigDecimal.multiply(entrust1.getStockEntrustMoney());
                    tUserClient.topup(entrust1.getStockEntrustUserid(),bigDecimal.multiply(entrust1.getStockEntrustMoney()));

                    //卖出方=持仓()
                    pos1.setStockPosTradblance(pos1.getStockPosTradblance()-(entrust1.getStockEntrustNum()-entrust1.getStockEntrustTransnum())); //修改持有总数量(总数-(卖出数量-卖出成交))
                    pos1.setStockPosDjblance(pos1.getStockPosDjblance()-(entrust1.getStockEntrustNum()-entrust1.getStockEntrustTransnum()));  //修改冻结数量(冻结数量-(卖出-成交))
                    posUpdate(pos1); //修改或删除 持仓
                    //买入方=持仓;部成entrust2
                    POS posm1=new POS();
                    posm1.setStockPosUserid(entrust2.getStockEntrustUserid());  //set用户id
                    posm1.setStockPosCode(entrust2.getStockEntrustCode());  //set股票代码
                    List<POS> posmm1=iPosService.selectPos1(posm1);
                    if (posmm1.size()==0){ //买方未持有此股票,[创建持仓]
                        posm1.setStockPosKyblance(entrust1.getStockEntrustNum()-entrust1.getStockEntrustTransnum());  //可用=卖出数量-卖出成交(成交前)
                        posm1.setStockPosCompany(entrust2.getStockEntrustCompany());   //股票名
                        posm1.setStockPosMoney(entrust2.getStockEntrustMoney());     //成本价
                        posm1.setStockPosTradblance(entrust1.getStockEntrustNum()-entrust1.getStockEntrustTransnum());  //持有总数=卖出数量-卖出成交(成交前)
                        posm1.setStockPosDjblance(0);  //冻结数
                        posAdd(posm1);//添加持仓
                    } else {    //买方已持有此股票,[修改持仓]
                        POS posmm= posmm1.get(0);
                        //((持有总数*成本价)+(成交数*委托价))%(持有总数+成交数)
                        int big=posmm.getStockPosMoney().intValue(); //成本(成交前)
                        int big1=entrust2.getStockEntrustMoney().intValue(); //委托价
                        int money=((posmm.getStockPosTradblance()*big)+(entrust1.getStockEntrustNum()*big1))/(posmm.getStockPosTradblance()+entrust1.getStockEntrustNum());
                        BigDecimal a = new BigDecimal(money);
                        posmm.setStockPosMoney(a);  //修改成本价
                        posmm.setStockPosTradblance(posmm.getStockPosTradblance()+(entrust1.getStockEntrustNum()-entrust1.getStockEntrustTransnum())); //持有总数=总数+(卖出委托数量-卖出成交)
                        posmm.setStockPosKyblance(posmm.getStockPosKyblance()+(entrust1.getStockEntrustNum()-entrust1.getStockEntrustTransnum())); //可用=可用+(卖出委托数量-卖出成交)
                        posUpdate(posmm); //修改或删除 持仓
                    }

                    //买入=委托entrust2
                    entrust2.setStockEntrustStatus("部成");  //委托状态
                    entrust2.setStockEntrustTransnum(entrust2.getStockEntrustTransnum()+(entrust1.getStockEntrustNum()-entrust1.getStockEntrustTransnum()));  //成交数量=成交数量+(卖出委托数量-卖出成交)
                    int e=iEntrustService.updateStatus(entrust2);
                    //  卖出=修改委托
                    Entrust entrust3=new Entrust();
                    entrust3.setStockEntrustId(entrust1.getStockEntrustId());  //委托主键
                    entrust3.setStockEntrustStatus("全部成交");  //委托状态
                    entrust3.setStockEntrustTransnum(entrust1.getStockEntrustNum());   //成交数量=委托数量
                    int s=iEntrustService.updateStatus(entrust3);
                    break;
                    // 买卖数量相等
                }if (entrust1.getStockEntrustNum()-entrust1.getStockEntrustTransnum() == entrust2.getStockEntrustNum()-entrust2.getStockEntrustTransnum()) {
                    // [双方全部成交(停止for循环)] 修改双方: 委托信息,成交信息,持仓信息 资金
                    //  卖出=成交
                    Deal deal=new Deal(entrust1.getStockEntrustUserid(),
                            entrust1.getStockEntrustCode(),
                            entrust1.getStockEntrustCompany(),
                            "卖出",
                            entrust1.getStockEntrustMoney(),
                            new SimpleDateFormat("yyy-MM-dd HH:mm:ss.SSS").format(new Timestamp(System.currentTimeMillis())),
                            entrust1.getStockEntrustContractid());
                    dealAdd(deal);
                    //  买入=成交entrust2
                    Deal deal1=new Deal(entrust2.getStockEntrustUserid(),
                            entrust2.getStockEntrustCode(),
                            entrust2.getStockEntrustCompany(),
                            "买入",
                            entrust2.getStockEntrustMoney(),
                            new SimpleDateFormat("yyy-MM-dd HH:mm:ss.SSS").format(new Timestamp(System.currentTimeMillis())),
                            entrust2.getStockEntrustContractid());
                    dealAdd(deal1);

//                    修改卖出方账户金额(===) 本次交易数量*委托金额
                    BigDecimal bigDecimal=new BigDecimal(entrust1.getStockEntrustNum()-entrust1.getStockEntrustTransnum());
                       bigDecimal.multiply(entrust1.getStockEntrustMoney());
                    tUserClient.topup(entrust1.getStockEntrustUserid(),bigDecimal.multiply(entrust1.getStockEntrustMoney()));

                    //卖出方=持仓
                    pos1.setStockPosTradblance(pos1.getStockPosTradblance()-(entrust1.getStockEntrustNum()-entrust1.getStockEntrustTransnum())); //修改持有总数量(总数-(卖出数量-卖出成交))
                    pos1.setStockPosDjblance(pos1.getStockPosDjblance()-(entrust1.getStockEntrustNum()-entrust1.getStockEntrustTransnum()));  //修改冻结数量(冻结数量-(卖出-成交))
                    posUpdate(pos1); //修改或删除 持仓
                    //买入方=持仓entrust2
                    POS posm1=new POS();
                    posm1.setStockPosUserid(entrust2.getStockEntrustUserid());  //set用户id
                    posm1.setStockPosCode(entrust2.getStockEntrustCode());  //set股票代码
                    List<POS> posmm1=iPosService.selectPos1(posm1);
                    if (posmm1.size()==0){
                        //买方未持有此股票,[创建持仓]
                        posm1.setStockPosKyblance(entrust2.getStockEntrustNum());  //可用
                        posm1.setStockPosCompany(entrust2.getStockEntrustCompany());   //股票名
                        posm1.setStockPosMoney(entrust2.getStockEntrustMoney());     //成本价
                        posm1.setStockPosTradblance(entrust2.getStockEntrustNum());  //持有总数
                        posm1.setStockPosDjblance(0);  //冻结数
                        posAdd(posm1);//添加持仓
                    } else {

                        //买方已持有此股票,[修改持仓]
                        POS posmm=posmm1.get(0);
                        //((持有总数*成本价)+(成交数*委托价))%(持有总数+成交数)
                        int big=posmm.getStockPosMoney().intValue(); //成本(成交前)
                        int big1=entrust2.getStockEntrustMoney().intValue(); //委托价
                        int money=((posmm.getStockPosTradblance()*big)+(entrust1.getStockEntrustNum()*big1))/(posmm.getStockPosTradblance()+entrust1.getStockEntrustNum());
                        BigDecimal a = new BigDecimal(money);
                        posmm.setStockPosMoney(a);
                        posmm.setStockPosTradblance(posmm.getStockPosTradblance()+(entrust1.getStockEntrustNum()-entrust1.getStockEntrustTransnum()));//持有总数=持有总数+(卖出委托数-成交数)
                        posmm.setStockPosKyblance(posmm.getStockPosKyblance()+(entrust1.getStockEntrustNum()-entrust1.getStockEntrustTransnum())); //可用数=可用数+(卖出委托数-成交数)
                        posUpdate(posmm); //修改或删除 持仓
                    }

                    //  卖出=修改委托
                    Entrust entrust3=new Entrust();
                    entrust3.setStockEntrustId(entrust1.getStockEntrustId()); //委托主键
                    entrust3.setStockEntrustStatus("全部成交");  //委托状态
                    entrust3.setStockEntrustTransnum(entrust1.getStockEntrustNum());   //成交数量=委托数量
                    int s=iEntrustService.updateStatus(entrust3);
                    //  买入=委托entrust2
                    entrust2.setStockEntrustStatus("全部成交");  //委托状态
                    entrust2.setStockEntrustTransnum(entrust2.getStockEntrustNum());  //成交数量=委托数量
                    int e=iEntrustService.updateStatus(entrust2);
                    break;
                    //卖出数量大于买入数量
                } else {
//                    [卖方部分成交 买方全部成交(循环继续)] 修改双方: 委托信息,成交信息,持仓信息 资金
                    //  卖出=成交
                    Deal deal=new Deal(entrust1.getStockEntrustUserid(),
                            entrust1.getStockEntrustCode(),
                            entrust1.getStockEntrustCompany(),
                            "卖出",
                            entrust1.getStockEntrustMoney(),
                            new SimpleDateFormat("yyy-MM-dd HH:mm:ss.SSS").format(new Timestamp(System.currentTimeMillis())),
                            entrust1.getStockEntrustContractid());
                    dealAdd(deal);
                    //  买入=成交entrust2
                    Deal deal1=new Deal(entrust2.getStockEntrustUserid(),
                            entrust2.getStockEntrustCode(),
                            entrust2.getStockEntrustCompany(),
                            "买入",
                            entrust2.getStockEntrustMoney(),
                            new SimpleDateFormat("yyy-MM-dd HH:mm:ss.SSS").format(new Timestamp(System.currentTimeMillis())),
                            entrust2.getStockEntrustContractid());
                    dealAdd(deal1);


//                    修改卖出方账户金额(>>>) 本次交易数量*委托金额
                    BigDecimal bigDecimal=new BigDecimal(entrust2.getStockEntrustNum()-entrust2.getStockEntrustTransnum());
                    bigDecimal.multiply(entrust1.getStockEntrustMoney());
                    tUserClient.topup(entrust1.getStockEntrustUserid(),bigDecimal.multiply(entrust1.getStockEntrustMoney()));


                    //卖出方;[部分成交]=修改持仓
                    pos1.setStockPosTradblance(pos1.getStockPosTradblance()-(entrust2.getStockEntrustNum()-entrust2.getStockEntrustTransnum())); //修改持有数量(总数-(买方买入数量-已成交数量))
                    pos1.setStockPosDjblance(pos1.getStockPosDjblance()-(entrust2.getStockEntrustNum()-entrust2.getStockEntrustTransnum())); //修改冻结数量(冻结数量-(买方买入数量-已成交数量))
                    posUpdate(pos1); //修改或删除 持仓


                    //买入方;[全部成交]=持仓
                    POS posm1=new POS();
                    posm1.setStockPosUserid(entrust2.getStockEntrustUserid());  //set用户id
                    posm1.setStockPosCode(entrust2.getStockEntrustCode());  //set股票代码
                    List<POS> posmm1=iPosService.selectPos1(posm1);
                    if (posmm1.size()==0){
                        //买方未持有此股票,[创建持仓]
                        posm1.setStockPosKyblance(entrust2.getStockEntrustNum());  //可用数量(买方委托数量)
                        posm1.setStockPosCompany(entrust2.getStockEntrustCompany());   //股票名
                        posm1.setStockPosMoney(entrust2.getStockEntrustMoney());     //成本价
                        posm1.setStockPosTradblance(entrust2.getStockEntrustNum());  //持有总数(买方委托数量)
                        posm1.setStockPosDjblance(0);  //冻结数
                        posAdd(posm1);//添加持仓
                    } else {
                        //买方已持有此股票,[修改持仓]
                        POS posmm=posmm1.get(0);
                        //((持有总数*成本价)+(成交数*委托价))%(持有总数+成交数)
                        int big=posmm.getStockPosMoney().intValue(); //成本(成交前)
                        int big1=entrust2.getStockEntrustMoney().intValue(); //委托价
                        int money=((posmm.getStockPosTradblance()*big)+(entrust2.getStockEntrustNum()*big1))/(posmm.getStockPosTradblance()+entrust2.getStockEntrustNum());
                        BigDecimal a = new BigDecimal(money);
                        posmm.setStockPosMoney(a);
                        posmm.setStockPosTradblance(posmm.getStockPosTradblance()+entrust2.getStockEntrustNum()-entrust2.getStockEntrustTransnum());//持有总数+成交数
                        posmm.setStockPosKyblance(posmm.getStockPosKyblance()+entrust.getStockEntrustNum()-entrust2.getStockEntrustTransnum()); //可用数+成交数
                        posUpdate(posmm); //修改或删除 持仓
                    }

                    //  卖出=修改委托
                    Entrust entrust3=new Entrust();
                    entrust3.setStockEntrustId(entrust1.getStockEntrustId()); //委托主键
                    entrust3.setStockEntrustStatus("部成");  //委托状态
                    entrust3.setStockEntrustTransnum(entrust1.getStockEntrustTransnum()+(entrust2.getStockEntrustNum()-entrust2.getStockEntrustTransnum()));   //成交数量(成交数量+买方委托数量-成交数量(本次成交前))
                    int s=iEntrustService.updateStatus(entrust3);

                    //修改判断条件成交数量
                    if (entrust1.getStockEntrustTransnum()==0||entrust1.getStockEntrustTransnum()==null){
                        entrust1.setStockEntrustTransnum(entrust2.getStockEntrustNum()-entrust2.getStockEntrustTransnum());
                    } else {
                        entrust1.setStockEntrustTransnum(entrust1.getStockEntrustTransnum()-(entrust2.getStockEntrustNum()-entrust2.getStockEntrustTransnum()));
                    }
                    //  买入=修改委托
                    entrust2.setStockEntrustStatus("全部成交");  //委托状态
                    entrust2.setStockEntrustTransnum(entrust2.getStockEntrustNum());  //成交数量=委托数量
                    int e=iEntrustService.updateStatus(entrust2);

                }
            }
        }
        return resultBean1;
    }

    /**
     *  查询 委托b信息(状态 :未成交,部成)
     */
    @RequestMapping("/keywordEntrust")
    public ResultBean keywordEntrust(){
        int userid=10086;
        return new ResultBean(101,"查询 未成交和部分成交的委托信息成功!",iEntrustService.keywordEntrust(userid));
    }

    /**
     *  查询 委托b信息(状态 :未成交,部成)  带分页
     */
    @RequestMapping("/keywordEntrust2")
    public ResultBean keywordEntrust2(int pageIndex,int pageSize,int userid,String stockEntrustCode){
        return new ResultBean(101,"查询分页成功",iEntrustService.keywordEntrust2(userid,pageIndex,pageSize,stockEntrustCode));
    }

    /**
     *  查询 委托b信息 带分页 条件查询
     */
    @RequestMapping("/keywordEntrust3")
    public ResultBean keywordEntrust3(int pageIndex,int pageSize,int userid,@RequestBody(required = false) Entrust entrust){
        return new ResultBean(101,"查询分页成功",iEntrustService.keywordEntrust3(userid,pageIndex,pageSize,entrust));
    }

    /**
     *  查询 根据委托ID查询一条委托信息
     */
    @RequestMapping("/selectEntrustById")
    public ResultBean selectEntrustById(int stockEntrustId){
        return new ResultBean(101,"查询成功",iEntrustService.getOneEntrust(stockEntrustId));
    }

    /**
     * [卖出]查询 持仓信息(一条)
     */
    @RequestMapping("/selectPos")
    public ResultBean selectPos(@RequestBody POS pos) {   //  股票代码
        POS pos1=null;
        List<POS> posList=iPosService.selectPos1(pos);
        if (posList.size()==0){
            return new ResultBean(500, "无数据","无数据");
        } else {
            pos1=posList.get(0);
            return new ResultBean(101,"获取数据成功",pos1);
        }
    }

    /**
     *  添加 持仓
     */
    @RequestMapping("/posAdd")
    public ResultBean posAdd(POS pos){
        return new ResultBean(101,"保存成功",iPosService.posAdd(pos)) ;
    }

    /**
     *  修改或删除 持仓
     */
    @RequestMapping("/posUpdate")
    public ResultBean posUpdate(@RequestBody POS pos){
        return new ResultBean(101,"保存成功",iPosService.posUpdate(pos));
    }


}
